package com.inspur.his.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.his.constant.Constant;
import com.inspur.his.pojo.Drugs;
import com.inspur.his.pojo.RoExtExt;
import com.inspur.his.pojo.User;
import com.inspur.his.pojo.Ky;
import com.inspur.his.service.DrugsService;
import com.inspur.his.service.ZdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Controller
public class ZdController {
    @Autowired
    ZdService zdService;
    @RequestMapping("zdListController")
    public String docList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        //获取页面提交来的查询条件
        String roid = request.getParameter("roid");
        String memberid = request.getParameter("memberid");
        String createdate = request.getParameter("createdate");
        String suserid ="";
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("suser");
        if(null != user)
         suserid = user.getId();
        System.out.println("11111==========="+suserid);
        //  点击医生管理左侧菜单进行默认无条件查询
        HashMap map = new HashMap();
        map.put("roid", roid);
        map.put("memberid", memberid);
        map.put("createdate",createdate);
        map.put("suserid",suserid);
        PageHelper.startPage(pn, 5);
        List<RoExtExt> zdList = zdService.zdList(map);
        PageInfo<RoExtExt> pageInfo = new PageInfo<RoExtExt>(zdList, zdList.size());
        model.addAttribute("zdList", zdList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("roid", roid);
        model.addAttribute("memberid", memberid);
        model.addAttribute("createdate",createdate);
        model.addAttribute("suserid",suserid);
        return "doc/zdList.jsp";
    }
    @RequestMapping("zdAddController")
    public String zdAdd(RoExtExt roExtExt, Model model) throws Exception {
        //参数绑定 user对象中已经绑定了表单中的用户信息(确认sql中插入的所有字段均有值吗？）
        //思考主键的生成方式--多种1 MySQL中的主键自增
        Date day = new Date();
        SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-DD");
        String today = fm.format(day);
        roExtExt.setOptime(fm.parse(today));
        zdService.zdAdd(roExtExt);
        model.addAttribute("roExtExt", roExtExt);
        model.addAttribute("success", "医生信息保存成功！");
        return "doc/zdadd.jsp";
    }
    @RequestMapping("deleteZdController")
    public  String deleteZd(String id,Model model) {
        zdService.deleteZdById(id);
        return "redirect:zdListController";
    }

    @RequestMapping("ZdkyController")
    public  String kaiyao(String id,Model model) {
        System.out.println("id=="+id);
        List<Ky> kyList = zdService.getkyById(id);
        PageInfo<Ky> pageInfo = new PageInfo<Ky>(kyList, kyList.size());
        model.addAttribute("roid", id);
        model.addAttribute("kyList", kyList);
        return "doc/kyList.jsp";
    }

    @RequestMapping("ZdkyAddController")
    public  String kyAdd(Ky ky, Model model) {
        String id = UUID.randomUUID().toString();
        ky.setStatus(Constant.USER_STATUS_ABLE);
        ky.setId(id);
        zdService.kyAdd(ky);
        return "redirect:ZdkyController?id="+ky.getRoid();
    }

    @Autowired
    DrugsService drugsService;
    @RequestMapping("drugsList")
    @ResponseBody
    public List<Drugs> docInfo(HttpServletRequest req, Model md) {
        HashMap map = new HashMap<String, String>();
        String drugsname =req.getParameter("drugsname");
        map.put("drugname",drugsname);
        List<Drugs> drugsList = drugsService.drugsList(map);
        return drugsList;
    }

    @RequestMapping("kyAdd")
    public String ky(String roid, Model model) {
        model.addAttribute("roid", roid);
        return "doc/kyAdd.jsp";
    }

}
